﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BabyFood.Entities;
using BabyFood.Data;

public partial class dct_QuangCao_CNQuangCao : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        imgViewer.ImageUrl = H_AnhDaiDien.Value.Length > 0 ? Common.sPathUpQuangCaoThumbail + H_AnhDaiDien.Value : "~/Images/no_image.jpg";
        if (!Page.IsPostBack)
        {
            BindCatalog.BindNhomQuangCao(cboNhomQuangCao);
            if (Request.QueryString["id"] != null)
            {
                H_CatalogId.Value = Request.QueryString["id"].ToString();
                BindData(Convert.ToInt32(H_CatalogId.Value));
            }
        }
    }

    protected void BindData(int IdQuangCao)
    {
        QuangCao entQuangCao = DataRepository.QuangCaoProvider.GetById(IdQuangCao);
        cboNhomQuangCao.SelectedValue = entQuangCao.IdNhomQuangCao.ToString();
        txtTenQC.Text = entQuangCao.TenQuangCao;
        chkTinNong.Checked = entQuangCao.TrangThai;
        imgViewer.ImageUrl = entQuangCao.AnhDaiDien.Length > 0 ? Common.sPathUpQuangCaoThumbail +"/"+ entQuangCao.AnhDaiDien : "~/Images/no_image.jpg";
        txtLienKet.Text = entQuangCao.DiaChi;
        txtDienGiai.Text = entQuangCao.DienGiai;
    }

    protected void cmdLuu_Click(object sender, ImageClickEventArgs e)
    {
        bool updateStatus = false;
        try
        {
            QuangCao entQuangCao = new QuangCao();
            entQuangCao.Id = Convert.ToInt32(H_CatalogId.Value);
            entQuangCao.IdNhomQuangCao = Convert.ToInt32(cboNhomQuangCao.SelectedValue);
            entQuangCao.TenQuangCao = Common.StandardString(txtTenQC.Text);
            entQuangCao.DiaChi = Common.StandardString(txtLienKet.Text);
            entQuangCao.DienGiai = Common.StandardString(txtDienGiai.Text);
            if (H_AnhDaiDien.Value.Length == 0)
            {
                entQuangCao.AnhDaiDien = "~/Images/no_image.jpg";
            }
            else
            {
                entQuangCao.AnhDaiDien = Common.StandardString(H_AnhDaiDien.Value);
            }

            entQuangCao.TrangThai = chkTinNong.Checked;
            if (entQuangCao.Id == 0)
            {
                updateStatus = DataRepository.QuangCaoProvider.Insert(entQuangCao);
                cmdNhapTiep_Click(sender, e);
            }
            else
            {

                entQuangCao.EntityState = EntityState.Changed;
                updateStatus = DataRepository.QuangCaoProvider.Update(entQuangCao);
                cmdNhapTiep_Click(sender, e);
            }

            if (!updateStatus)
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.QUANGCAO_CAPNHAT_THATBAI, true);
            else
            {
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THANHCONG, Constants.QUANGCAO_CAPNHAT_THANHCONG, true);
                H_CatalogId.Value = entQuangCao.Id.ToString();
            }

        }
        catch (Exception ex)
        {
            Common.WriteLog("CNQuangCao.aspx.cs", "cmdLuu_Click", ex.ToString());
            ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.QUANGCAO_CAPNHAT_THATBAI, true);
        }
    }

    protected void cmdNhapTiep_Click(object sender, ImageClickEventArgs e)
    {
        H_CatalogId.Value = "0";
        imgViewer.ImageUrl = "~/Images/no_image.jpg";
        H_AnhDaiDien.Value = "~/Images/no_image.jpg";
        cboNhomQuangCao.SelectedValue = "0";
        txtTenQC.Text = "";
        txtLienKet.Text = "";
        chkTinNong.Checked = true;
        txtDienGiai.Text = "";
    }
    
}